package com.handinhand.mobile;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class HomeActivity extends BaseActivity {

	private Button logoutButton, communitiesButton;
	private TextView nameTV, surnameTV,usernameTV,emailTV;
	final Context context = this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);

		setupUI();
	}

	private void setupUI() {
		nameTV = (TextView) findViewById(R.id.tvName);
		nameTV.setText("Name:\t" + getApp().getLoggedUser().getName());		
		surnameTV = (TextView) findViewById(R.id.tvSurname);
		surnameTV.setText("Surname:\t" + getApp().getLoggedUser().getSurname());
		usernameTV = (TextView) findViewById(R.id.tvUsername);
		usernameTV.setText("username:\t" + getApp().getLoggedUser().getUsername());
		emailTV = (TextView) findViewById(R.id.tvEmail);
		emailTV.setText("Email:\t" + getApp().getLoggedUser().getEmail());

		communitiesButton = (Button) findViewById(R.id.btnCommunities);
		communitiesButton.setOnClickListener(onCommunitiesButtonClick);

		logoutButton = (Button) findViewById(R.id.btnLogout);
		logoutButton.setOnClickListener(onLogoutButtonClick);

	}
	private OnClickListener onCommunitiesButtonClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(HomeActivity.this, CommunitiesActivity.class);
			startActivity(intent);
			finish();
		}
	};

	private OnClickListener onLogoutButtonClick = new OnClickListener() {
		@Override
		public void onClick(View v) {

			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
			alertDialogBuilder.setTitle("Are you sure?");
			alertDialogBuilder.setMessage("Do you really want to logout?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int id) {
					getApp().setAuthToken(null);
					getApp().setLoggedUser(null);

					Intent intent = new Intent(HomeActivity.this, MainActivity.class);
					startActivity(intent);
					finish();
				}
			}).setNegativeButton("No", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});

			AlertDialog alertDialog = alertDialogBuilder.create();
			alertDialog.show();
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.home, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
